<template>
  <common-layout>
    <div class="top">
      <div class="header">
        <img alt="logo" class="logo" src="@/assets/img/logo.png" />
        <span class="title">{{systemName}}</span>
      </div>
      <div class="desc">后台管理平台</div>
    </div>
    <div class="login">
      
      <a-form @submit="onSubmit" :form="form">

  
         
        <a-form-item>
          <a-input
            autocomplete="autocomplete"
            size="large"
            placeholder=""
            v-decorator="['name', {rules: [{ required: true, message: '请输入昵称', whitespace: true}]}]"
          >
            <a-icon slot="prefix" type="user" />
          </a-input>
        </a-form-item>
        <a-form-item>
          <a-input
            size="large"
            placeholder=""
            autocomplete="autocomplete"
            type="password"
            v-decorator="['password1', {rules: [{ required: true, message: '请输入密码', whitespace: true}]}]"
          >
            <a-icon slot="prefix" type="lock" />
          </a-input>
        </a-form-item>
        <a-form-item>
          <a-input
            size="large"
            placeholder=""
            autocomplete="autocomplete"
            type="password"
            v-decorator="['password2', {rules: [{ required: true, message: '请输入相同的密码', whitespace: true}]}]"
          >
            <a-icon slot="prefix" type="lock" />
          </a-input>
        </a-form-item>
        <a-form-item>
          <div style="display: flex;align-items: center">
            <a-input
              type="code"
              v-decorator="['code2', {rules: [{ required: true, message: '请输入验证码', whitespace: true}]}]" size="large" placeholder="captcha" style="margin-right: 12px">
              <a-icon slot="prefix" type="code" />
            </a-input>

            <img style="height: 38px;width: auto;border-radius: 4px" src="/Api/api/v1/code.do?abc=2313423" alt="" >
          </div>
        </a-form-item>
        <a-form-item>
          <a-button :loading="logging" style="width: 100%;margin-top: 24px" size="large" htmlType="submit" type="primary">注册</a-button>
        </a-form-item>
      </a-form>
    </div>
  </common-layout>
</template>

<script>
import CommonLayout from '@/layouts/CommonLayout'
import { register } from '@/services/user'
import {mapMutations} from 'vuex'

export default {
  name: 'Register',
  components: {CommonLayout},
  data () {
    return {
      logging: false,
      error: '',
      form: this.$form.createForm(this)
    }
  },
  computed: {
    systemName () {
      return this.$store.state.setting.systemName
    }
  },
  methods: {
    ...mapMutations('account', ['setUser', 'setPermissions', 'setRoles']),
    onSubmit (e) {
      e.preventDefault()
      this.form.validateFields((err) => {
        if (!err) {
          this.logging = true
          const email = this.form.getFieldValue('email')
          const password = this.form.getFieldValue('password')
          const code = this.form.getFieldValue('code')
          const nickName = this.form.getFieldValue('nickName')
          register(email, password, code, nickName).then(this.validateEmail)
        }
      })
    },
    validateEmail(res) {
      console.log(res)
      this.logging = false
      const result = res.data.result
      if (res.data.errorCode === 0) {
        console.log(result)
        this.$router.push('/login')
      } else {
        this.error = res.data.msg
      }
    }
  }
}
</script>

<style lang="less" scoped>
  .common-layout{
    .top {
      text-align: center;
      .header {
        height: 44px;
        line-height: 44px;
        a {
          text-decoration: none;
        }
        .logo {
          height: 44px;
          vertical-align: top;
          margin-right: 16px;
        }
        .title {
          font-size: 33px;
          color: @title-color;
          font-family: 'Myriad Pro', 'Helvetica Neue', Arial, Helvetica, sans-serif;
          font-weight: 600;
          position: relative;
          top: 2px;
        }
      }
      .desc {
        font-size: 14px;
        color: @text-color-second;
        margin-top: 12px;
        margin-bottom: 40px;
      }
    }
    .login{
      width: 368px;
      margin: 0 auto;
      @media screen and (max-width: 576px) {
        width: 95%;
      }
      @media screen and (max-width: 320px) {
        .captcha-button{
          font-size: 14px;
        }
      }
      .icon {
        font-size: 24px;
        color: @text-color-second;
        margin-left: 16px;
        vertical-align: middle;
        cursor: pointer;
        transition: color 0.3s;

        &:hover {
          color: @primary-color;
        }
      }
    }
  }
</style>
